#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define width 640
#define height 480
int x=300,y=430,r=20;
int t=2,m=2;
char c;
int bl=100,br=300;
int a[3][8];
	
void ball1(){
   setcolor(BLACK);	
   setfillcolor(BLACK);
   fillcircle(x,y,r);
}
void ball2(){
   setcolor(BLACK);
   setfillcolor(BLUE);
   fillcircle(x,y,r);
}
void Drawbackground() {
   setfillcolor(BLACK);	
   fillrectangle(0,0,width,height);	
}
void board1(){
	setcolor(BLACK);
    setfillcolor(BLACK);
    fillrectangle(bl,450,br,470);
	
}
void board2(){
	setcolor(BLACK);
    setfillcolor(RED);
    fillrectangle(bl,450,br,470);

	
}
void setball(){
	
	if(x<=r||x>=width-r){
	   t=-t;
	}
	if(y==r){
	   m=-m;
	}
	if(x>=bl&&x<=br&&y==450-r){
	   m=-m;
	}
	
	int t1=x/80+1;
	int i;
    for(i=1;i<=3;i++){
		if((y-r)/20==i  && a[(y-r)/20-1][t1-1]==0){
			setcolor(BLACK);
            setfillcolor(BLACK);
            fillrectangle((t1-1)*80,(i-1)*20,t1*80,i*20);
			a[(y-r)/20-1][t1-1]=1;
			m=-m;
			break;
		}
		
	}
	if(y==height-r){
		initgraph(100, 200); 
	    Drawbackground();
		char s[] = "GAME  OVER";
        outtextxy(10, 20, s);
		
	
	}
	x+=t;
	y+=m;
}
void setboard(){
    if(_kbhit()){
	   c =_getch();
	   switch(c){
	   case 'a' :
		   if(bl<=0){
			   //bl = 0;
		       break;
		   }
		   else{
		   bl-=10;
		   br-=10;
		   break;
		   }
	   case 'd':
		   if(br>=640)
		   {
			   //br = 640;
			   break;
		   }
		   else{
		   br+= 10;
		   bl+=10;
		   break;
		   }
	   }
	   }
}
void show(){
	int i,j;
	srand((unsigned)time(NULL));
	for( i=0;i<3;i++){
		for(j=0;j<8;j++){
			int t=rand()%3;
			if(t==0){
				setcolor(BLACK);
                setfillcolor(RED);
                fillrectangle(j*80,i*20,(j+1)*80,(i+1)*20);
			}
			if(t==1){
				setcolor(BLACK);
                setfillcolor(GREEN);
                fillrectangle(j*80,i*20,(j+1)*80,(i+1)*20);
			}
			if(t==2){
				setcolor(BLACK);
                setfillcolor(BLUE);
                fillrectangle(j*80,i*20,(j+1)*80,(i+1)*20);
			}
		}

	
	}
	for(i;i>=0;i--){
		for(j;j>=0;j--){
			a[i][j]=0;
		}
	}
   
}
void main(){
	initgraph(width, height); 
	Drawbackground();	
	show();
	BeginBatchDraw();

    while(1){
	ball1();
	board1();
    setball();
	setboard();
    ball2();
	board2();
	FlushBatchDraw();
		
	Sleep(10);
	}
    
	 
	_getch();
	closegraph();
}